package com.tt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tt.entity.pojo.UserPO;
import com.tt.entity.vo.UserVO;
import com.tt.service.UserService;
import org.apache.catalina.User;
import org.apache.tomcat.util.buf.HexUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:12
 */
@RestController
public class UserController {

    private final UserService userServiceImpl;

    public UserController(UserService userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    @PostMapping("/register")
    public List<Object> register(String avatar, String email, String name, String pwd) {
        // 前端做参数校验，后端不做

        ArrayList<Object> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", null);
        // 邮箱是否存在
        UserPO tmpUserPO = userServiceImpl.getOne(new QueryWrapper<UserPO>().eq("email", email));
        if (tmpUserPO != null) {
            map.put("msg", "当前用户已存在");
            list.add(map);
            return list;
        }

        // SHA256加密密码
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        md.update(pwd.getBytes(StandardCharsets.UTF_8));
        pwd = HexUtils.toHexString(md.digest());


//        UserPO userPO = new UserPO(null, name, avatar, email, pwd, null, null, null, null);

        UserPO userPO = new UserPO();
        userPO.setName(name).setAvatar(avatar).setEmail(email).setPwd(pwd);
//        return userServiceImpl.save(userPO) ? new UserVO(userPO.getId(), name, avatar, email, userPO.getRole(), userPO.getGmtCreated()) : null;

        if (userServiceImpl.save(userPO)) {
            UserPO user = userServiceImpl.getById(userPO.getId());
            list.add(map);
            list.add(new UserVO(user.getId(), user.getName(), user.getAvatar(), user.getEmail(), user.getRole(), user.getGmtCreated()));
        } else {
            map.put("msg", "服务器错误, 用户插入失败");
        }

        return list;
    }

    @PostMapping("/login")
    public List<Object> login(String email, String pwd) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        md.update(pwd.getBytes(StandardCharsets.UTF_8));
        pwd = HexUtils.toHexString(md.digest());

        QueryWrapper<UserPO> wrapper = new QueryWrapper<>();
        wrapper.select("id, name, avatar, role, gmt_created")
                .eq("email", email)
                .eq("pwd", pwd);
        UserPO userPO = userServiceImpl.getOne(wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", null);
        if (userPO == null) {
            map.put("msg", "用户名或密码错误");
        }
        ArrayList<Object> list = new ArrayList<>();
        list.add(map);
        list.add(userPO);
        return list;
//        return userPO == null ? null : new UserVO(userPO.getId(), userPO.getName(), userPO.getAvatar(), email, userPO.getRole(), userPO.getGmtCreated());
    }
}
